package student.controller;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import student.beans.Student;
import student.service.StudentService;
import student.beans.Msg;

@Controller
public class StudentController {
	@Autowired
	StudentService studentService;
	/*public StudentController() {
		studentService=new StudentService();
	}*/
	@RequestMapping("/listStudents")
	@ResponseBody
	public Msg listStudents(@RequestParam(value="pageNo",defaultValue="1",required=false) Integer pageNo,@RequestParam(value="item",required=false) String item,@RequestParam(value="term",required=false) String term) {
		Student student=studentService.createStudentBean(item, term);
		PageInfo<Student> pageinfo=studentService.getPageInfoByItem(pageNo, student);	
		return Msg.success().addMap("PageInfo", pageinfo);
	}
	@RequestMapping("/addStudent")
	@ResponseBody
	public Msg addStudent(@Valid Student student,BindingResult result) {
		Map<String, Object> errorMap=new HashMap<>();
		if(result.hasErrors()){			
			List<FieldError> errors=result.getFieldErrors();
			for(FieldError e:errors) {
				errorMap.put(e.getField(), e.getDefaultMessage());				
			}
			return Msg.fail().addMap("errorsMessage", errorMap);
		}else {
			studentService.addOneStudent(student);
			return Msg.success();
		}	
	}
	
	@RequestMapping("/editStudent")
	@ResponseBody
	public Msg editStudent(@Valid Student student,BindingResult result) {
		Map<String, Object> errorsMap=new HashMap<>();
		if(result.hasErrors()) {
			List<FieldError> errors=result.getFieldErrors();
			for(FieldError e:errors) {
				errorsMap.put(e.getField(), e.getDefaultMessage());
			}
			return Msg.fail().addMap("errorsMessage", errorsMap);
		}else {
			studentService.editStudent(student);
			return Msg.success();
		}
		
	}
	@RequestMapping("/deleteStuById")
	@ResponseBody
	public Msg deleteStudent(String id) {
		if(id.contains("-")) {
			String[] idArr=id.split("-");
			List<Integer> stuIds=new ArrayList<>();
			for(String i:idArr) {
				stuIds.add(Integer.parseInt(i));
			}
			studentService.deleteStuByIds(stuIds);
		}else {
			int stuId=Integer.parseInt(id);
			studentService.deleteStuById(stuId);
		}		
		return Msg.success();
	}
	
	
	/*@RequestMapping("/search")
	public ModelAndView searchStudent(@RequestParam(value="item",defaultValue="Id") String item,@RequestParam("term") String term) {
		String url = "listStudents";
		ModelAndView mav=new ModelAndView();
		mav.setViewName(url);
		Student student=new Student();
		switch(item) {
		case "Id":
			student.setStuId(Integer.parseInt(term));
			break;
		case "name":
			student.setStuName("%"+term+"%");;
			break;
		case "phone":
			student.setPhone("%"+term+"%");
			break;
		}
		PageInfo<Student> pageinfo=studentService.getPageInfoByItem(1,student);
		Map<String,PageInfo<Student>> map=new HashMap<>();
		map.put("pageInfo", pageinfo);
		mav.addAllObjects(map);
		return mav;	
	}
*/
}
